//
//  SISSMineViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/13.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSMineViewController.h"
#import "SISSInfomationViewController.h"
#import "SISSStarsShowView.h"
#import "SISSNetworkingManager+Mine.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SISSMyReleaseCooperationViewController.h"
#import "SISSAboutUsWebViewController.h"
#import "SISSMyBrokerageViewController.h"
#import "SISSMyKPIViewController.h"

@interface SISSMineViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *checkHouseView;
@property (weak, nonatomic) IBOutlet SISSStarsShowView *starView;

@property (weak, nonatomic) IBOutlet UIImageView *headerImageView;
@property (weak, nonatomic) IBOutlet UIButton *roundButton;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

/* 个人信息 */
@property (strong, nonatomic) SISSPersonInfoModel *personInfo;

@end

@implementation SISSMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.starView.stars = 0;
//    [self requestPeronInfo];
    [self requestCooperationRequestCount];
    self.starView.isInfo = YES;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    headerImageView.image = [UIImage imageNamed:@"mine_bg"];
    
//    self.tableView.tableHeaderView = headerImageView;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    
//    SISSTabBarController *tabbar = (SISSTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//    tabbar.customTabBar.hidden = NO;
    
    [self requestPeronInfo];
    [self requestCooperationRequestCount];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
    
    self.checkHouseView.backgroundColor = [UIColor whiteColor];    
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    
    return YES;
}


- (IBAction)longPressGestureAction:(UILongPressGestureRecognizer *)sender {
    
//    self.checkHouseView.backgroundColor = [UIColor redColor];
}

- (IBAction)checkHouseTapAction:(UITapGestureRecognizer *)sender {

    SISSAboutUsWebViewController *vc = [[SISSAboutUsWebViewController alloc] init];
    vc.title = @"房源核验";
//    vc.url = @"ris.szpl.gov.cn/MLS/Index.aspx";
//    vc.url = @"zjj.sz.gov.cn:8004/publicity/?type=fybm";
    vc.url = @"zjj.sz.gov.cn/ris/szfdc/MLS/Index.aspx";
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)evaluateTapAction:(UITapGestureRecognizer *)sender {
    SISSAboutUsWebViewController *vc = [[SISSAboutUsWebViewController alloc] init];
    vc.title = @"房产估价";
//    vc.url = @"zjj.sz.gov.cn:8004/publicity/?type=fybm";
//    vc.url = @"pgj.szpgzx.com/cx.jsp";
     vc.url = @"61.144.226.83:8010/jgcx/index.jsp";
    // http://61.144.226.83:8010/jgcx/index.jsp
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    if (indexPath.section == 1 && indexPath.row == 0) {
//        SISSMyBrokerageViewController *vc = [[SISSMyBrokerageViewController alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
//    } else
        if (indexPath.section == 2 && indexPath.row == 0) {
        SISSMyKPIViewController *vc = [[SISSMyKPIViewController alloc] init];
        vc.title = @"我的业绩指标";
        [self.navigationController pushViewController:vc animated:YES];
    }
}

// 为适配iPhoneX
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 70;
    } else if (indexPath.section == 4 && indexPath.row == 4) {
        return UI_IS_IPHONE_X ? 64 : 20;
    }
    return 44;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offset = scrollView.contentOffset.y;
//    NSLog(@"%@",@(offset));
    CGRect rect = self.headerImageView.frame;
    
    if (offset <= 0) {
        rect.origin.y = offset;
        rect.size.height = 200 - offset;
        self.headerImageView.frame = rect;
    }
    if (offset < -120) {
        [self requestPeronInfo];
        [self requestCooperationRequestCount];
    }

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if ([segue.identifier isEqualToString:@"CheckHouse"]) {
        
        self.checkHouseView.backgroundColor = [UIColor redColor];
    }
    
    if ([segue.identifier isEqualToString:@"SISSInfomationViewController"]) {
        
        SISSInfomationViewController *infoVC = (SISSInfomationViewController *)segue.destinationViewController;
        infoVC.personInfo = self.personInfo;
        infoVC.spId = [SISSUserAccount shared].spId;
    }
}

#pragma mark - 获取个人信息
- (void)requestPeronInfo {

    // 头像等赋值
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:[SISSUserAccount shared].portrait] placeholderImage:[UIImage imageNamed:@"头像"]];
    self.nameLabel.text = [SISSUserAccount shared].name;
    self.starView.stars = [SISSUserAccount shared].starLevel;
    
    [SISSPublicSingleton shared].isHud = YES;
    [kNetworkManager requestPersonInfoWithSpId:[SISSUserAccount shared].spId success:^(SISSPersonInfoModel *personInfo) {
        
        self.personInfo = personInfo;
        
        [SISSUserAccount shared].portrait = personInfo.portrait;
        [SISSUserAccount shared].name = personInfo.name;
        [SISSUserAccount shared].license_no = personInfo.license_no;
        [SISSUserAccount shared].idNo = personInfo.idNo;
        [SISSUserAccount shared].starLevel = personInfo.star_level;
        
        // 头像等赋值
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:personInfo.portrait] placeholderImage:[UIImage imageNamed:@"头像"]];
        self.nameLabel.text = personInfo.name;
        self.starView.stars = personInfo.star_level;
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

#pragma mark - 获取合作请求数量
- (void)requestCooperationRequestCount {
    
    [kNetworkManager requestCooperationRequestCountyWithsuccess:^(NSInteger count) {
        
        self.roundButton.hidden = count == 0;
        [self.roundButton setTitle:[NSString stringWithFormat:@"%zd",count] forState:UIControlStateNormal];
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

@end
